package com.user.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.user.utils.*;
import java.util.Iterator;
import org.springframework.web.bind.annotation.RequestMapping;
import com.user.bean.*;
import com.user.service.UserService;

/*控制层：UserController*/
@Controller
@RequestMapping("/user")
public class UserController {

//	注入UserService对象
	@Autowired
	private UserService userService;
	@Autowired
	private HttpServletRequest request;

//	根据userInsert.jsp页面传递的数据，封装在user对象中插入数据库，重定向到重定向到findAll请求
	@RequestMapping("/insert")
	public String insert(User user) {
		String role = (String) request.getSession().getAttribute("role");
		try {
			userService.insert(user);
			if (role == null) {
				request.setAttribute("msg", "注册成功");
				return "/userRegister.jsp";
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (role == null) {
				request.setAttribute("msg", "注册失败");
				return "/userRegister.jsp";
			}
			throw new RuntimeException("添加失败");
		}
		return "redirect:findAll";
	}
}